/**
 * 平台相关功能 Composable
 */
import { computed } from 'vue'
import { useAppStore } from '../store/app'
import {
  checkNetwork,
  getOSType,
  getPlatformName,
  getPlatformType,
  getTerminalType,
  isAndroid,
  isIOS,
  isWechatBrowser,
  isWechatEnvironment,
  isWechatMiniProgram,
} from '../utils/platform'

export function usePlatform() {
  const appStore = useAppStore()

  // 计算属性
  const platformType = computed(() => getPlatformType())
  const platformName = computed(() => getPlatformName())
  const terminalType = computed(() => getTerminalType())
  const osType = computed(() => getOSType())

  const isH5 = computed(() => appStore.isH5)
  const isApp = computed(() => appStore.isApp)
  const isMiniProgram = computed(() => appStore.isMiniProgram)
  const isWechat = computed(() => appStore.isWechat)
  const isWechatH5 = computed(() => appStore.isWechatH5)
  const isDevelopment = computed(() => appStore.isDevelopment)

  // 平台判断方法
  const checkPlatform = () => {
    return {
      type: platformType.value,
      name: platformName.value,
      terminal: terminalType.value,
      os: osType.value,
      isH5: isH5.value,
      isApp: isApp.value,
      isMiniProgram: isMiniProgram.value,
      isWechat: isWechat.value,
      isWechatH5: isWechatH5.value,
      isWechatBrowser: isWechatBrowser(),
      isWechatMiniProgram: isWechatMiniProgram(),
      isWechatEnvironment: isWechatEnvironment(),
      isIOS: isIOS(),
      isAndroid: isAndroid(),
      isDevelopment: isDevelopment.value,
    }
  }

  // 检查网络状态
  const checkNetworkStatus = async () => {
    return await checkNetwork()
  }

  // 获取平台特定配置
  const getPlatformConfig = () => {
    const platform = platformType.value

    // 根据不同平台返回不同配置
    const configs = {
      'h5': {
        title: '网页版',
        features: ['share', 'navigation'],
        limitations: [],
      },
      'h5-wechat': {
        title: '微信浏览器',
        features: ['share', 'jssdk', 'pay'],
        limitations: [],
      },
      'app': {
        title: '原生应用',
        features: ['push', 'camera', 'location'],
        limitations: [],
      },
      'mp-weixin': {
        title: '微信小程序',
        features: ['login', 'pay', 'share'],
        limitations: ['no-web-api'],
      },
      'mp-alipay': {
        title: '支付宝小程序',
        features: ['login', 'pay'],
        limitations: ['no-web-api'],
      },
    }

    return configs[platform] || configs.h5
  }

  return {
    // 计算属性
    platformType,
    platformName,
    terminalType,
    osType,
    isH5,
    isApp,
    isMiniProgram,
    isWechat,
    isWechatH5,
    isDevelopment,

    // 方法
    checkPlatform,
    checkNetworkStatus,
    getPlatformConfig,
  }
}
